import requests


class Tieba:
    def __init__(self):
        # 爬取的地址
        self.url = 'https://tieba.baidu.com/f?kw=python'
        # 定义请求头
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'
        }

    def get_data(self):
        # 获取数据的方法
        response = requests.get(self.url, headers=self.headers)
        return response.content.decode('utf-8')

    def process_data(self, data):
        # 处理数据
        with open('tieba_python.html', 'w', encoding='utf-8')as f:
            f.write(data)

    def start(self):
        # 获取数据
        data = self.get_data()
        # 处理数据
        self.process_data(data)


if __name__ == '__main__':
    t = Tieba()
    t.start()
